// 跳转页面
function login(){
    location.href = "./pages/login.html"
}

function register(){
    location.href = "./pages/register.html"
}

function user(){
    location.href = "./pages/user.html"
}

function search(type,b_search){
    type = document.getElementById("category").value;
    b_search = document.getElementById("search").value
    // let a = document.getElementById('search_result')
    // console.log(obj.value)
    let str = b_search.replace(/(^\s*)|(\s*$)/g, '');//去除空格;
 
    if (str == '' || str == undefined || str == null) {
        //return true;
        console.log('空')
        // alert("no!")
        location.href="./index.html"
    } else {
        //return false;
        console.log('非空');
        localStorage.setItem('type', type);
        localStorage.setItem('b_search', b_search);
        location.href = "./pages/search.html"
        // $.ajax({
        //     url: 'http://81.68.240.146:5256/book/book_q/',
        //     type: 'POST',
        //     data: JSON.stringify({          
        //         'type': type,
        //         'b_search':b_search
        //     }),
        //     contentType:"application/JSON;charset=UTF-8",
        //     dataType:'json',
        //     Headers:{
        //         withCredential:true
        //     },
        //     success: function(res) { // 接口调用成功回调函数
        //         // data 为服务器返回的数据
        //         console.log(res)
        //         // a.innerHTML=res.data[0].data.b_name
        //     },
        //     Error:function(data){
        //         alert('no')
        //     }
        // });
    }
}
// 监听进度条
window.onscroll = function(){
    let t = document.documentElement.scrollTop || document.body.scrollTop; 
    let header = document.getElementById("header") 
    let little_nav = document.getElementById("little_nav")
	if( t >= 200 ) { 
	    // alert("滚动条滚动了300距离")
        header.style.display = "none"
        little_nav.style.display = "block"
    }
    else{
        header.style.display = "block"
        little_nav.style.display = "none"

    }
}
// 点击出现导航栏
$(".snav-menu").click(function(){
    console.log($("ul").css)
    $("ul").css("display","block")
    console.log($("ul").css)
})


// 轮播图

let left = document.querySelector(".left-arr")
let right = document.querySelector(".right-arr")
let min = document.querySelectorAll(".banner-index")
let hotimgs = document.querySelector(".banner-imgs")

let time;
let index=0;

// 功能
function position(){
    hotimgs.style.left = (index * (-100)) + "%"
}
function add(){
    if(index >= min.length-1){
        index = 0
    }
    else{
        index++
    }
}
function back(){
    if(index < 1){
        index = min.length-1
    }
    else{
        index--
    }
}
// lunbo
function timer(){
    time = setInterval(()=>{
        index++
        // min[index].style.backgroundColor = '#fff'
        // min[index-1].style.backgroundColor = 'rgba(204,204,204,0.8)'
        if(index > min.length-1){
            index = 0
            // min[index].style.backgroundColor = '#fff'
            // min[min.length-1].style.backgroundColor = 'rgba(204,204,204,0.8)'
        }
        add()
        back()
        position()
    },2000)
}

for(let i =0;i<min.length;i++){
    min[i].addEventListener("click",()=>{
        index = i
        position()
        clearInterval(time)
        timer()
    })
}
// 开始轮播
timer()
// 左右按钮
left.addEventListener('click',()=>{
    back()

    position()
    clearInterval(time)
    timer()
})
right.addEventListener('click',()=>{
    add()

    position()
    clearInterval(time)
    timer()
})

// 数字yd
function showNum(controlName, _num) {
    var control = $(controlName);
    var showRunning = 0;
    var showEnd = _num;
    var runningSpeed = parseInt(_num / 300); //调节越大，速度越慢
    control.text("0");
    var _play = setInterval(function () {
        if (_num-showRunning<20) {
            runningSpeed = 1;
        }
        control.text(showRunning);
        showRunning += runningSpeed;
        //if (showRunning > (showEnd * 0.95) && showRunning < showEnd) {
        //    setInterval(_play, 20);
        //    continue
        //}
      
        if (showRunning > showEnd ) {
            clearInterval(_play);
        }
    }, 1);
}
showNum("#book",406);
showNum("#member",378);
